#include "io.h"

#ifndef UGLY_HACK_FOR_THOSE_WHO_DONT_KNOW_HOWTO_INSTALL_SDL
#include <SDL/SDL.h>
#else
#include <SDL.h>
#endif

#include "../gfx/gfx.h"
#include "../sfx/sfx.h"

static int _Players[2] = {0, 0};
static SDL_Joystick *_io_joysticks[2];

void io_init(void)
{
    SDL_InitSubSystem(SDL_INIT_JOYSTICK);
    for(int i = 0; i < SDL_NumJoysticks() && i < 2; i++)
    {
        _io_joysticks[i] = SDL_JoystickOpen(i);
        log_printf(LOG_INFO, "Joystick #%d (%s) found", i, SDL_JoystickName(i));
    }

    log_puts(LOG_INFO, "Done initializing io module");
}

void io_exit(void)
{
    for(int i = 0; i < SDL_NumJoysticks() && i < 2; i++)
    {
        SDL_JoystickClose(_io_joysticks[i]);
    }
    SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
    log_puts(LOG_INFO, "Done uninitializing io module");
}

int io_player_get_actions(int player)
{
    return _Players[player];
}

void io_player_read_actions(void)
{
    int keys_size;
    Uint8 *keys = SDL_GetKeyState(&keys_size);
    _Players[0] = _Players[1] = 0;
    SDL_PumpEvents();

    // Joueur 1
    if(keys[SDLK_a])                    _Players[0] |= PLAYER_LEFT;
    if(keys[SDLK_d])                    _Players[0] |= PLAYER_RIGHT;
    if(keys[SDLK_w])                    _Players[0] |= PLAYER_UP;
    if(keys[SDLK_s])                    _Players[0] |= PLAYER_DOWN;
    if(keys[SDLK_g])                    _Players[0] |= PLAYER_A;
    if(keys[SDLK_SPACE])                _Players[0] |= PLAYER_B;
    if(keys[SDLK_ESCAPE])               _Players[0] |= PLAYER_START;

    // Joueur 2
    if(keys[SDLK_LEFT])                 _Players[1] |= PLAYER_LEFT;
    if(keys[SDLK_RIGHT])                _Players[1] |= PLAYER_RIGHT;
    if(keys[SDLK_UP])                   _Players[1] |= PLAYER_UP;
    if(keys[SDLK_DOWN])                 _Players[1] |= PLAYER_DOWN;
    if(keys[SDLK_BACKSPACE])            _Players[1] |= PLAYER_A;
    if(keys[SDLK_RETURN])                _Players[1] |= PLAYER_B;
    //if(keys[SDLK_ESCAPE])               _Players[1] |= PLAYER_START;

    // Read joystick
    for(int i = 0; i < SDL_NumJoysticks() && i < 2; i++)
    {
        Uint8 hat = SDL_JoystickGetHat(_io_joysticks[i], 0);
        if(hat & SDL_HAT_UP)    _Players[i] |= PLAYER_UP;
        if(hat & SDL_HAT_DOWN)  _Players[i] |= PLAYER_DOWN;
        if(hat & SDL_HAT_LEFT)  _Players[i] |= PLAYER_LEFT;
        if(hat & SDL_HAT_RIGHT) _Players[i] |= PLAYER_RIGHT;

        if(SDL_JoystickGetButton(_io_joysticks[i], 2))  _Players[i] |= PLAYER_A;
        if(SDL_JoystickGetButton(_io_joysticks[i], 7))  _Players[i] |= PLAYER_B;
        if(SDL_JoystickGetButton(_io_joysticks[i], 9))  _Players[i] |= PLAYER_START;
    }

}

int io_player_get_buttons_menu(void)
{
    io_player_read_actions();

    static int player_key_old;
    int player_key_current;
    int player_key;

    player_key_current = io_player_get_actions(1);
    player_key = ~player_key_old & player_key_current;
    player_key_old = player_key_current;

    return player_key;
}

void io_load_ressources(void)
{
    // Game images
    gfx_image_load("data/gfx/soldier.bmp");
    gfx_image_load("data/gfx/soldier_altcolor.bmp");
    gfx_image_load("data/gfx/bat.bmp");
    gfx_image_load("data/gfx/fruits.bmp");
    gfx_image_load("data/gfx/Map.bmp");
    gfx_image_load("data/gfx/lifebar.bmp");
    gfx_image_load("data/gfx/superbar.bmp");
    gfx_image_load("data/gfx/keys.bmp");

    // Menu images
    gfx_image_load("data/gfx/font.bmp");
    gfx_image_load("data/gfx/minotaure_final.bmp");
    gfx_image_load("data/gfx/roueoptn.bmp");
    gfx_image_load("data/gfx/soundon.bmp");
    gfx_image_load("data/gfx/soundoff.bmp");
    gfx_image_load("data/gfx/laby.bmp");

    // Load Sound
    sfx_load_sound("data/sfx/menu_keyenter.wav", 0);
    sfx_load_sound("data/sfx/battement.wav", 1);
    sfx_load_sound("data/sfx/bravo.wav", 2);
    sfx_load_sound("data/sfx/jesuismalade.wav", 3);
    sfx_load_sound("data/sfx/menu_depla.wav", 4);
    sfx_load_sound("data/sfx/ouuuh.wav", 5);
    sfx_load_sound("data/sfx/rire_diabolique.wav", 6);
    sfx_load_sound("data/sfx/toux.wav", 7);
}
